package com.seven.wxmessagepush.utils;

import com.seven.wxmessagepush.entity.ImageMessage;
import com.seven.wxmessagepush.entity.TextMeaasge;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @Author:Seven Description:
 * @Date:Created at 2022/8/17 11:35
 */
public class SendDataUtil {

    /**
     * 推送图片消息方法
     * @param imageMessage
     * @return
     */
    public String sendImageData(ImageMessage imageMessage) {
        String data = new JsonString().getImageJson(imageMessage.getBase64(),imageMessage.getMd5());
        return sendPost(imageMessage.getUrl(),data);
    }

    /**
     * 推送文字消息方法
     * @param textMeaasge
     * @return
     */
    public String sendTextData(TextMeaasge textMeaasge){
        String data = new JsonString().getTextJson(textMeaasge.getText());
        return sendPost(textMeaasge.getUrl(),data);
    }

    /**
     * 推送markdown消息方法
     * @param textMeaasge
     * @return
     */
    public String sendMarkdownData(TextMeaasge textMeaasge){
        String data = new JsonString().getMarkDownJson(textMeaasge.getText());
        return sendPost(textMeaasge.getUrl(),data);
    }

    /**
     * 推送news消息方法
     * @param textMeaasge
     * @return
     */
    public String sendNewsData(TextMeaasge textMeaasge){
        String data = new JsonString().getNewsJson(textMeaasge.getText());
        return sendPost(textMeaasge.getUrl(),data);
    }

    /**
     * 发送网络请求的方法
     * @param url
     * @param param
     * @return
     */
    public String sendPost(String url, String param) {
        OutputStreamWriter out = null;
        BufferedReader reader = null;
        String response = "";
        try {
            URL httpUrl = null;
            httpUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) httpUrl.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("connection", "keep-alive");
            conn.setUseCaches(false);
            conn.setInstanceFollowRedirects(true);
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.connect();
            out = new OutputStreamWriter(conn.getOutputStream());
            out.write(param);
            out.flush();
            reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String lines;
            while ((lines = reader.readLine()) != null) {
                lines = new String(lines.getBytes(), "utf-8");
                response += lines;
            }
            reader.close();
            conn.disconnect();
        } catch (Exception e) {
            return "{\n" +
                    "    \"errcode\": 1,\n" +
                    "    \"errmsg\": \"推送消息异常，异常请联系系统管理员！异常信息：\"\n"+e.getMessage()+
                    "}";
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return response;
    }

}
